import axios from 'axios'
// import * as types from './mutation-types.js'
/* 异步操作 */
export default {
  noScrollFn ({commit, state}, bool) {
    state.noScrollFlag = bool
  },
  //  cookie取出来的格式：'key1=value1; key2=value2; key3=value3'
  getCookie ({commit, state}, key) {
    // console.log(document.cookie)
    // commit('GETCOOKIE', key)
    // return localStorage.getItem(key)?localStorage.getItem(key):null
    if (localStorage.getItem(key)) {
      return localStorage.getItem(key)
    } else {
      // 查找一个值，可以把它转换为数组，通过循环、对比来查找
      var arr = document.cookie.split('; ')// 不要忘记空格!!!
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          if (arr[i].indexOf('ylUserInfo=') !== -1) {
            if (arr[i] !== 'ylUserInfo=' || arr[i] !== 'ylUserInfo=;') {
              return arr[i].replace('ylUserInfo=', '')
            }
          }
        }
      } else {
        return null// 没有找到该cookie，返回null
      }
    }
  },

  // 继续付款
  goOnPay: ({commit, state}, payIdMasterId) => {
    // 授权code接口
    axios.post('//prem2.yaolaivip.com/api/wechat/code',
      require('qs').stringify({
        redirect_uri: state.transferUrl,
        state: payIdMasterId.payId + '-' + payIdMasterId.masterId + '+wxpay',
        isSilence: true
      })
    ).then(res => {
      console.log(res)
      if (res.data.code === '9999' || res.data.code === 9999) {
        // 访问返回的data(url)
        console.log(res.data.data)
        // window.location.href = res.data.data
      }
    }).catch((err) => {
      console.log(err)
    })
  },
  // 验证微信登录，获取code、跳转页面、state
  wxLogin ({commit, state}, wxParams) {
    // alert(window.location.hash)
    commit('WXLOGIN', wxParams)
  }
}
